package main;

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

import javax.swing.JTextField;

import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.LocatorImpl;

public class TaxContentHandler implements ContentHandler {
	private Locator locator;
	private Persons persons;
	private JTextField error;

	public TaxContentHandler(Persons ps, JTextField error) {
		persons = ps;
		locator = new LocatorImpl();
		this.error = error;  
	}

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		// TODO Auto-generated method stub

	}

	@Override
	public void endDocument() throws SAXException {
		// TODO Auto-generated method stub
		System.out.println("XML Parsing done");
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		// TODO Auto-generated method stub

	}

	@Override
	public void endPrefixMapping(String prefix) throws SAXException {
		// TODO Auto-generated method stub

	}

	@Override
	public void ignorableWhitespace(char[] ch, int start, int length)
			throws SAXException {
		// TODO Auto-generated method stub

	}

	@Override
	public void processingInstruction(String target, String data)
			throws SAXException {
		// TODO Auto-generated method stub
		System.out.println("Instruction de fonctionnement : " + target);
		System.out.println("  dont les arguments sont : " + data);
	}

	@Override
	public void setDocumentLocator(Locator locator) {
		// TODO Auto-generated method stub
		this.locator = locator;
	}

	@Override
	public void skippedEntity(String name) throws SAXException {
		// TODO Auto-generated method stub

	}

	@Override
	public void startDocument() throws SAXException {
		// TODO Auto-generated method stub
		System.out.println("Parsing xml File");
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes atts) throws SAXException {
		try {
			// TODO Auto-generated method stub
			if ("TAX".equalsIgnoreCase(localName)) {
				String nom = null;
				Long val = null;
				Date date = null;
				for (int index = 0; index < atts.getLength(); index++) {
					// on parcours la liste des attributs
					if (atts.getLocalName(index).equals("NOM")) {
						nom = atts.getValue(index);
					}
					if (atts.getLocalName(index).equals("VAL")) {
						val = Long.parseLong(atts.getValue(index));
					}
					if (atts.getLocalName(index).equals("DATE")) {
						DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, Locale.FRANCE);
						date = df.parse(atts.getValue(index));
					}
				}
				if (nom != null && val!=null && date!=null)
				{
					this.persons.addPerson(new Person(nom, val, date));
				}
					
			}
			if ("WISH".equalsIgnoreCase(localName)){
				String nom = null;
				String loot = null;
				String loot2 = "";
				String loot3 = "";
				String loot4 = "";
				String loot5 = "";
				String loot6 = "";
				String type = null;
				Date date = null;
				for(int index = 0;index < atts.getLength(); index++){
					if (atts.getLocalName(index).equalsIgnoreCase("NOM")){
						nom = atts.getValue(index);
					}
					if (atts.getLocalName(index).equalsIgnoreCase("LOOT")){
						loot = atts.getValue(index);
						loot = Loot.checkLoot(loot);
					}
					if (atts.getLocalName(index).equalsIgnoreCase("LOOT2")){
						loot2 = atts.getValue(index);
						loot2 = Loot.checkLoot(loot2);
						if (loot2 == null)
							loot2 = "";
					}
					if (atts.getLocalName(index).equalsIgnoreCase("LOOT3")){
						loot3 = atts.getValue(index);
						loot3 = Loot.checkLoot(loot3);
						if (loot3 == null)
							loot3 = "";
					}
					if (atts.getLocalName(index).equalsIgnoreCase("LOOT4")){
						loot4 = atts.getValue(index);
						loot4 = Loot.checkLoot(loot4);
						if (loot4 == null)
							loot4 = "";
					}
					if (atts.getLocalName(index).equalsIgnoreCase("LOOT5")){
						loot5 = atts.getValue(index);
						loot5 = Loot.checkLoot(loot5);
						if (loot5 == null)
							loot5 = "";
					}
					if (atts.getLocalName(index).equalsIgnoreCase("LOOT6")){
						loot6 = atts.getValue(index);
						loot6 = Loot.checkLoot(loot6);
						if (loot6 == null)
							loot6 = "";
					}
					if (atts.getLocalName(index).equalsIgnoreCase("TYPE")){
						type = atts.getValue(index);
						type = Loot.checkType(type);
					}
					if (atts.getLocalName(index).equalsIgnoreCase("DATE")){
						DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, Locale.FRANCE);
						date = df.parse(atts.getValue(index));
					}
				}
				if (nom != null && loot != null && date != null)
				{
					this.persons.addLootWish(new Loot(nom, loot, loot2, loot3, loot4, loot5, loot6, type, date));
				}
			}
			if ("DROPLOOT".equalsIgnoreCase(localName)){
				String nom = null;
				String loot = null;
				String type = null;
				Date date = null;
				for(int index = 0;index < atts.getLength(); index++){
					if (atts.getLocalName(index).equalsIgnoreCase("NOM")){
						nom = atts.getValue(index);
					}
					if (atts.getLocalName(index).equalsIgnoreCase("LOOT")){
						loot = atts.getValue(index);
						loot = Loot.checkLoot(loot);
					}
					if (atts.getLocalName(index).equalsIgnoreCase("TYPE")){
						type = atts.getValue(index);
						type = Loot.checkType(type);
					}
					if (atts.getLocalName(index).equalsIgnoreCase("DATE")){
						DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, Locale.FRANCE);
						date = df.parse(atts.getValue(index));
					}
				}
				if (nom != null && loot != null && type !=null && date != null)
				{
					this.persons.addDroppedLoot(new Loot(nom, loot, type, date));
				}
			}
			if ("RAIDPRESENCE".equalsIgnoreCase(localName)){
				String nom = null;
				Long nbraid = null;
				Date date = null;
				for(int index = 0;index < atts.getLength(); index++){
					if (atts.getLocalName(index).equalsIgnoreCase("NOM")){
						nom = atts.getValue(index);
					}
					if (atts.getLocalName(index).equalsIgnoreCase("NBRAID")){
						nbraid = Long.parseLong(atts.getValue(index));
					}
					if (atts.getLocalName(index).equalsIgnoreCase("DATE")){
						DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, Locale.FRANCE);
						date = df.parse(atts.getValue(index));
					}
				}
				if (nom != null && nbraid != null && date != null)
				{
					this.persons.addRaidPresence(new Person(nom, nbraid, date));
				}
			}
		} catch (Exception e) {
			if (error !=null)
				error.setText(e.getMessage());
			e.printStackTrace();
		}
	}

	@Override
	public void startPrefixMapping(String prefix, String uri)
			throws SAXException {
		// TODO Auto-generated method stub

	}

}
